package com.ruoyi.common.utils;

/**
 * @ClassName BinaryUtil
 * @Description TODO
 * @Author ZYJ
 * @Date 2024/1/17 13:46
 * @Version 1.0
 **/
public class BinaryUtil {

    /**
     * 10进制转2进制补全
     * @param num
     * @param size
     * @return
     */
    public static String decimalToBinary(int num, int size) {
        if (size <(Integer.SIZE - Integer.numberOfLeadingZeros(num))) {
            throw  new RuntimeException("传入size小于num二进制位数");
        }
        StringBuilder binStr = new StringBuilder();
        for(int i = size-1;i >= 0; i--){
            binStr.append(num >>> i & 1);
        }
        return binStr.toString();
    }
}
